package aip

import (
	. "com/aip/define"
)

/**
 * A class of variable serialization base on ByteCool.
 * 基于ByteCool之上的变量序列化类.
 *
 * @contributor amin2312(aminlab@qq.com)
 * @createDate 2014-09-15
 * @updateDate 2014-09-15
 */
type ByteVar struct {
	ByteCool
	/**
     * Last affected var type.
     * 最后影响的变量类型.
     */
	VarType byte
}

/**
 * New instance.
 * 创建实例.
 */
func NewByteVar(initSize int) *ByteVar {
	p := new(ByteVar)
	p.ByteVar(initSize)
	return p
}

/**
 * Constructor.
 * 构造函数.
 */
func (this *ByteVar) ByteVar(initSize int) {
	this.ByteCool.ByteCool(initSize)
}

/**
 * Write a byte variable.
 * 写入一个I8变量.
 */
func (this *ByteVar) WriteVarI8(value int8) {
	this.VarType = VAR_I8
	this.WriteUint8(this.VarType)
	this.WriteInt8(value)
}

/**
 * Write a unsigned byte variable.
 * 写入一个U8变量.
 */
func (this *ByteVar) WriteVarU8(value uint8) {
	this.VarType = VAR_U8
	this.WriteUint8(this.VarType)
	this.WriteUint8(value)
}

/**
 * Write a short variable.
 * 写入一个I16变量.
 */
func (this *ByteVar) WriteVarI16(value int16) {
	this.VarType = VAR_I16
	this.WriteUint8(this.VarType)
	this.WriteInt16(value)
}

/**
 * Write a unsigned short variable.
 * 写入一个U16变量.
 */
func (this *ByteVar) WriteVarU16(value uint16) {
	this.VarType = VAR_U16
	this.WriteUint8(this.VarType)
	this.WriteUint16(value)
}

/**
 * Write a int variable.
 * 写入一个I32变量.
 */
func (this *ByteVar) WriteVarI32(value int32) {
	this.VarType = VAR_I32
	this.WriteUint8(this.VarType)
	this.WriteInt32(value)
}

/**
 * Write a unsigned int variable.
 * 写入一个I32变量.
 */
func (this *ByteVar) WriteVarU32(value uint32) {
	this.VarType = VAR_U32
	this.WriteUint8(this.VarType)
	this.WriteUint32(value)
}

/**
 * Write a float variable.
 * 写入一个F32变量.
 */
func (this *ByteVar) WriteVarF32(value float32) {
	this.VarType = VAR_F32
	this.WriteUint8(this.VarType)
	this.WriteFloat32(value)
}

/**
 * Write a double variable.
 * 写入一个F64变量.
 */
func (this *ByteVar) WriteVarF64(value float64) {
	this.VarType = VAR_F64
	this.WriteUint8(this.VarType)
	this.WriteFloat64(value)
}

/**
 * Write a short error variable.
 * 写入一个E16变量.
 */
func (this *ByteVar) WriteVarE16(value uint16) {
	this.VarType = VAR_E16
	this.WriteUint8(this.VarType)
	this.WriteUint16(value)
}

/**
 * Write a binary variable.
 * 写入一个字节数组.
 */
func (this *ByteVar) WriteVarBin(value *ByteCool) {
	this.VarType = VAR_BIN
	this.WriteUint8(this.VarType)
	this.WriteLenBin(value)
}

/**
 * Write a utf8 string variable.
 * 写入一个字符串.
 */
func (this *ByteVar) WriteVarUTF8(value string) {
	this.VarType = VAR_UTF8
	this.WriteUint8(this.VarType)
	this.WriteLenUTF8(value)
}

/**
 * Write a array of bytes variable.
 * 写入多个字节.
 */
func (this *ByteVar) WriteVarBytes(value []byte) {
	this.VarType = VAR_BYTES
	this.WriteUint8(this.VarType)
	this.WriteLenBytes(value)
}

/**
 * Read a variable depend on the byte of variable type.
 * 读取一个变量.
 */
func (this *ByteVar) ReadVar() interface{} {
	if this.GetBytesReadable() == 0 {
		return nil
	}
	this.VarType = this.ReadUint8()
	if this.VarType == VAR_I8 {
		return this.ReadInt8()
	} else if this.VarType == VAR_U8 {
		return this.ReadUint8()
	} else if this.VarType == VAR_I16 {
		return this.ReadInt16()
	} else if this.VarType == VAR_U16 {
		return this.ReadUint16()
	} else if this.VarType == VAR_I32 {
		return this.ReadInt32()
	} else if this.VarType == VAR_U32 {
		return this.ReadUint32()
	} else if this.VarType == VAR_F32 {
		return this.ReadFloat32()
	} else if this.VarType == VAR_F64 {
		return this.ReadFloat64()
	} else if this.VarType == VAR_E16 {
		return this.ReadInt16()
	} else if this.VarType == VAR_STR {
		return this.ReadLenUTF8()
	} else if this.VarType == VAR_BYTES {
		return this.ReadLenBytes()
	} else if this.VarType == VAR_BIN {
		return this.ReadLenBin()
	} else {
		println("unknown var type:", this.VarType)
	}
	return nil
}
